<?php 
namespace App\Services;
use App\Services\Wxpay\Wxpay;
use QrCode;

trait PayTrait
{	

	///web端支付代码返回支付二维码
	public function WxwebPay($order){
		
		$wx=new Wxpay();
		$result=$wx->native('深触我心',$order->order_no, bcmul($order->amount, 100),route('wxpay.notify'));

		 //使用二维码包生成支付二维码图片
		$path = '/assets/qrcode/'.md5($result['code_url']).'.png';
        $qrcode_url = public_path($path);
        if(!file_exists(dirname($qrcode_url)))$this->create_folders(dirname($qrcode_url));
        if(!file_exists(dirname($path)))$this->create_folders(dirname($path));

        QrCode::format('png')->size(500)->generate($result['code_url'],$qrcode_url);
        $code_url=asset($path);
		return $code_url;
	}


	//h5
	public function Wxh5Pay($order){
		$wx=new Wxpay();
		$result=$wx->h5('深触我心',$order->order_no, bcmul($order->amount, 100),route('wxpay.notify'));
		$result['mweb_url'] .= '&redirect_url='. urlencode(route('mobile.order.blank', ['order'=> $order->order_no]));
		return $result;
	}

	//js
	public function WxjsPay($order,$openid){
		$wx=new Wxpay();
		$result=$wx->js('深触我心', $order_no->order_no,bcmul($order->amount, 100),route('wxpay.notify'), $openid);
		$url = route('mobile.wxpay.return', ['order'=> $order->order_no]);
        return $result['params'];
	}


	//ali  web支付
	public function alipayWebOrder($order)
    {   
        /*$order->pay_type="alipay_native";
        $order->save();*/
        $alipay = app('alipay.web');
        $alipay->setOutTradeNo($order->order_no);
        $alipay->setTotalFee($order->amount);
        $alipay->setSubject('深触我心');
        $alipay->setBody($order->product_name);

        $alipay->setQrPayMode('5'); //该设置为可选1-5，添加该参数设置，支持二维码支付。

        // 跳转到支付页面。
        return $alipay->getPayLink();
    }

    //手机端支付
    public function alipayWap($order){
        /*$order->pay_type="alipay_native";
        $order->save();*/
        $alipay = app('alipay.wap');
        $alipay->setOutTradeNo($order->order_no);
        $alipay->setTotalFee($order->amount);
        $alipay->setSubject('深触我心');
        $alipay->setBody($order->product_name);

        // 跳转到支付页面。
        return $alipay->getPayLink();
    }

    //app支付
    public function alipayAppOrder(User $user, Product $product)
    {
        /*$order = $this->createOrder($user, $product);
        $order->pay_type="alipay_app";
        $order->save();
        $this->order = $order;*/
        // 创建支付单。
        $alipay = app('alipay.app');
        $alipay->setOutTradeNo($order->order_no);
        $alipay->setTotalFee($order->amount);
        $alipay->setSubject('深触我心');
        $alipay->setBody($order->product_name);

        // 返回签名后的支付参数给支付宝移动端的SDK。
        return $alipay->getPayPara();
    }

	//没有文件夹则创建
    public function create_folders($dir){ 
        return is_dir($dir) or ($this->create_folders(dirname($dir)) and mkdir($dir, 0777, true)); 
    }

}
?>